package com.xingkeduo;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.JsonNode;
import com.gexin.rp.sdk.base.IAliasResult;
import com.gexin.rp.sdk.base.IPushResult;
import com.gexin.rp.sdk.base.IQueryResult;
import com.gexin.rp.sdk.base.impl.AppMessage;
import com.gexin.rp.sdk.base.impl.SingleMessage;
import com.gexin.rp.sdk.base.impl.Target;
import com.gexin.rp.sdk.exceptions.RequestException;
import com.gexin.rp.sdk.http.IGtPush;
import com.gexin.rp.sdk.template.LinkTemplate;
import com.gexin.rp.sdk.template.TransmissionTemplate;
import com.gexin.rp.sdk.template.style.Style0;
import com.xingkeduo.dto.WebsocketMessageResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;

import java.time.Instant;
import java.util.ArrayList;
import java.util.List;

@Slf4j
public class AppPush {

    //采用"Java SDK 快速入门"， "第二步 获取访问凭证 "中获得的应用配置，用户可以自行替换
    private static String appId = "zfi3wuQaB491C8qzgCBuM9";
    private static String appKey = "QePlO0qKQA66IxxTg9m6y3";
    private static String masterSecret = "0aUlkLuUoR9LOdYusWFEB3";
    private static String host = "http://sdk.open.api.igexin.com/apiex.htm";

    private static IGtPush push = new IGtPush(host, appKey, masterSecret);


    //将构造器设置为private禁止通过new进行实例化
    private AppPush() {
    }


    public static IPushResult pushMessage(String identificationCode, WebsocketMessageResponse websocketMessageResponse) {
        log.info("pushMessage:{},websocketMessageResponse:{}", identificationCode, websocketMessageResponse);

        websocketMessageResponse.setTimestamp(Instant.now().toEpochMilli());

        TransmissionTemplate template = new TransmissionTemplate();
        template.setAppId(appId);
        template.setAppkey(appKey);
        // 透传消息设置，1为强制启动应用，客户端接收到消息后就会立即启动应用；2为等待应用启动
        template.setTransmissionType(2);
        template.setTransmissionContent(JSON.toJSONString(websocketMessageResponse));
        SingleMessage message = new SingleMessage();
        message.setOffline(true);
        // 离线有效时间，单位为毫秒，可选
        message.setOfflineExpireTime(5 * 60 * 1000);
        message.setData(template);
        // 可选，1为wifi，0为不限制网络环境。根据手机处于的网络情况，决定是否下发
        message.setPushNetWorkType(0);
        Target target = new Target();
        target.setAppId(appId);

        if (StringUtils.isNotBlank(identificationCode)) {
            target.setClientId(identificationCode);
        } else {
            target.setAlias(websocketMessageResponse.getStoreId());
        }

        IPushResult ret = null;
        try {
            ret = push.pushMessageToSingle(message, target);
        } catch (RequestException e) {
            e.printStackTrace();
            ret = push.pushMessageToSingle(message, target, e.getRequestId());
        }
        if (ret != null) {
            log.info("pushMessage response:{}",JSON.toJSONString(ret));
        }
        return ret;
    }


    /**
     * 4. 获取用户状态
     * @param clientId
     * @return
     */
    public static boolean getUserStatus(String clientId) {
        log.info("getUserStatus:{}", clientId);
        if(StringUtils.isBlank(clientId)){
            return false;
        }
        IQueryResult abc = push.getClientIdStatus(appId, clientId);
        log.info("{}", abc.getResponse());
        Object result = abc.getResponse().get("result");
        return "Online".equals(result.toString());
    }


    public static void main(String[] args) {

        boolean b = getUserStatus("a0819d2c75067715769596285d882d3b");
        boolean status = getUserStatus("8d122f6c2e048e7596f3d41e4948fea8");
        boolean status1 = getUserStatus("123");

        System.out.println(b);
        System.out.println(status);
        System.out.println(status1);


    }



    public static void pushAllMessage(WebsocketMessageResponse websocketMessageResponse) {
        TransmissionTemplate template = new TransmissionTemplate();
        template.setAppId(appId);
        template.setAppkey(appKey);
        // 透传消息设置，1为强制启动应用，客户端接收到消息后就会立即启动应用；2为等待应用启动
        template.setTransmissionType(2);
        template.setTransmissionContent(JSON.toJSONString(websocketMessageResponse));
        // 设置定时展示时间
        // template.setDuration("2015-01-16 11:40:00", "2015-01-16 12:24:00");
        List<String> appIds = new ArrayList<String>();
        appIds.add(appId);

        // 定义"AppMessage"类型消息对象，设置消息内容模板、发送的目标App列表、是否支持离线发送、以及离线消息有效期(单位毫秒)
        AppMessage message = new AppMessage();
        message.setData(template);
        message.setAppIdList(appIds);
        message.setOffline(true);
        message.setOfflineExpireTime(1000 * 600);

        IPushResult ret = push.pushMessageToApp(message);

        if (ret != null) {
            log.info(JSON.toJSONString(ret));
        }
    }





    public static void bindAlias(String alias, String clientId) {
        log.info("bindAlias:{},{}", alias, clientId);
        IAliasResult iAliasResult = push.bindAlias(appId, alias, clientId);
        log.info(JSON.toJSONString(iAliasResult));
    }


    public static void unbindAlias(String alias, String clientId) {
        IAliasResult iAliasResult = push.unBindAlias(appId, alias, clientId);
        log.info(JSON.toJSONString(iAliasResult));
    }
}
